package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppConfig;
import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.Board;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleService;
import com.ABdolphin.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/article")
@RestController
@Tag(name = "文章接口")
@Slf4j
public class ArticleController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;

    @Operation(summary = "发布新帖")
    @PostMapping("/create")
    public AppResult create (HttpServletRequest request,
                             @Parameter(name = "boardId",description = "版块Id")@RequestParam("boardId") @NotNull Long boardId,
                             @Parameter(name = "title",description = "标题")@RequestParam("title") @NotBlank String title,
                             @Parameter(name = "content",description = "文章内容")@RequestParam("content") @NotBlank String content){
        //校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //版块的校验
        Board board = boardService.selectById(boardId.longValue());
        if (board==null||board.getDeleteState()==1||board.getState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        //封装文章对象
        Article article=new Article();
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        article.setTitle(title);
        article.setContent(content);
        articleService.create(article);
        return AppResult.success();
    }
    @Operation(summary = "获得帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@Parameter(name = "boardId",description = "版块Id")
                                                    @RequestParam(value = "boardId" ,required = false) Long boardId){
        List<Article> articles;
        if (boardId==null){
            articles = articleService.selectAll();
        }else {
            articles=articleService.selectAllByBoardId(boardId);
        }
        if (articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }
    @Operation(summary = "根据帖子id获得详情")
    @GetMapping("/details")
    public  AppResult<Article> getDetails(HttpServletRequest request,
                                          @Parameter(name = "id",description = "帖子Id")
                                          @RequestParam(value = "id" ,required = false) Long id){
        //拦截器已起作用,不用再次验证user的非空性
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectDetailById(id);
        if (article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断当前用户是否为作者
        if(user.getId()== article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }
    @Operation(summary = "修改帖子")
    @PostMapping("/modify")
    //request获得当前的用户状态(是否可以修改帖子),是不是作者
    public AppResult modify(HttpServletRequest request ,
                            @Parameter(name = "id",description = "帖子Id")@RequestParam("id") @NotNull Long id,
                            @Parameter(name = "title",description = "帖子标题")@RequestParam("title") @NotNull String title,
                            @Parameter(name = "content",description = "帖子正文")@RequestParam("content") @NotNull String content){
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if (article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断是否为作者
        if (user.getId()!= article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //查询帖子的状态
        if (article.getState()==1||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        articleService.modify(id,title,content);
        log.info("帖子更新成功,Article id="+id+", title= "+title+", content= "+content);
        return AppResult.success();
    }
    @Operation(summary = "点赞")
    @PostMapping("/thumbUp")
    public AppResult thumbUp(HttpServletRequest request,
                             @Parameter(name = "id",description = "帖子id")@RequestParam("id") @NotNull Long id){
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.thumbUpById(id);
        return AppResult.success();
    }
    @Operation(summary = "删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                             @Parameter(name = "id",description = "帖子id")@RequestParam("id") @NotNull Long id){
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if (article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断是否为作者
        if (user.getId()!= article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }
    @Operation(summary = "获得用户帖子的列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                    @Parameter(name = "userId",description = "用户id")@RequestParam(value = "userId",required = false) Long userId){
        if (userId==null){
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }

        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}




























